package za.org.jcicada.comms.impl.katcp;

import java.net.URI;
import java.nio.channels.SocketChannel;

import za.org.jcicada.comms.IConnection;
import za.org.jcicada.comms.IKatcpSerializerStrategy;
import za.org.jcicada.comms.impl.tcp.TcpConnector;

public class KatcpConnector extends TcpConnector {
    
	protected final IKatcpSerializerStrategy katcpSerializer;

    public KatcpConnector(String host, int port, IKatcpSerializerStrategy katcpSerializer) {
        super(host, port);
        this.katcpSerializer = katcpSerializer;
    }
    
    public KatcpConnector(URI locationURI, IKatcpSerializerStrategy katcpSerializer) {
        this(locationURI.getHost(), locationURI.getPort(), katcpSerializer);
		if (!locationURI.getScheme().equals(KatcpAcceptor.KATCP)) {
			throw new IllegalArgumentException(String.format("Scheme must be '%s' but was '%s'", KatcpAcceptor.KATCP, locationURI.getScheme()));
		}
    }

    @Override
    protected IConnection createConnection(SocketChannel channel) {
        return new KatcpConnection(channel, katcpSerializer);
    }
}
